﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.Workflow;

namespace SPDWorkflowStringActions
{
    public class Entry : Activity
    {

        public string errorMessage = "";

        public Entry()
        {
            this.Name = "Entry";
        }
        public static DependencyProperty InParam1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam1", typeof(string), typeof(Entry));

        public static DependencyProperty InParam2Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam2", typeof(string), typeof(Entry));

        public static DependencyProperty InParam3Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam3", typeof(string), typeof(Entry));

        public static DependencyProperty OutResult1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "OutResult1", typeof(string), typeof(Entry));


        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam1
        {
            get
            {
                return ((string)(base.GetValue(Entry.InParam1Property)));
            }
            set
            {
                base.SetValue(Entry.InParam1Property, value);
            }
        } // public string InParam1

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam2
        {
            get
            {
                return ((string)(base.GetValue(Entry.InParam2Property)));
            }
            set
            {
                base.SetValue(Entry.InParam2Property, value);
            }
        } // public string InParam2

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam3
        {
            get
            {
                return ((string)(base.GetValue(Entry.InParam3Property)));
            }
            set
            {
                base.SetValue(Entry.InParam3Property, value);
            }
        } // public string InParam3

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutResult1
        {
            get
            {
                return ((string)(base.GetValue(Entry.OutResult1Property)));
            }
            set
            {
                base.SetValue(Entry.OutResult1Property, value);
            }
        } // public string OutResult1


        protected override ActivityExecutionStatus
          Execute(ActivityExecutionContext executionContext)
        {

            bool errorOccurred = false;

            // Added 11/09/07
            // "entry()" returns the n^th entry in a delimited string.
            // InParam1: A delimited string.
            // InParam2: Index in the list of entries (0 = first entry)
            // InParam3: Delimiter to use (e.g. a comma).
            // Example: 
            //   InParam1: a,b,c,d,e,f
            //   InParam2: 3
            //   InParam3: ,
            //   OutResult1: "d"
            // Tested: NO
            try
            {
                // Create an "entries" array.  Because I'm a bit lazy,
                // initialize the length to the size of the unparsed
                // string.  There cannot be any more entries that that.
                string[] entries = new string[InParam1.Length];

                try
                {
                    // Load up the supplied delimiter
                    char[] splitter = new char[1];
                    splitter[0] = Convert.ToChar(InParam3);

                    // Split the incoming array into pieces.
                    entries = this.InParam1.Split(splitter);

                    try
                    {
                        // Get the entry we wanted.
                        OutResult1 = entries[Convert.ToInt32(InParam2)];
                    }
                    catch (Exception e)
                    {
                        this.errorMessage = "Failed to Convert or otherwise use InParam2:  " +
                            e.ToString();
                        errorOccurred = true;
                    } // catch error on convert.ToInt32
                }
                catch (Exception e)
                {
                    this.errorMessage = "Failed to split using the supplied delimiter. " +
                        e.ToString();
                    errorOccurred = true;
                } // catch 

            } // overall try
            catch (Exception e)
            {
                this.errorMessage = "Failed to initialize entries array: " + e.ToString();
                errorOccurred = true;

            } // catch 

            if (errorOccurred) throw new Exception(this.errorMessage);

            return ActivityExecutionStatus.Closed;
        }
        protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
        {
            try
            {
                ((ISharePointService)executionContext.GetService(typeof(ISharePointService))).LogToHistoryList(
                    base.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowError, 0,
                    TimeSpan.MinValue, string.Empty,
                    string.Format("Error occured when determining Entry value: {0}", exception.Message), string.Empty);

            }
            catch (Exception ex)
            { }
            return base.HandleFault(executionContext, exception);
        }
    }
}
